package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxw.nonghe.entity.FamilyPayment;
import com.lxw.nonghe.mapper.FamilyPaymentMapper;
import com.lxw.nonghe.service.FamilyPaymentService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 家庭缴费记录表 服务实现类
 * </p>
 *
 * @author lxw
 * @since 2025-05-15
 */
@Service
public class FamilyPaymentServiceImpl extends ServiceImpl<FamilyPaymentMapper, FamilyPayment> implements FamilyPaymentService {

    @Override
    public Page<FamilyPayment> getPaymentPage(int current, int size, Integer familyId, Integer year, String payerName) {
        QueryWrapper<FamilyPayment> wrapper = new QueryWrapper<>();

        // 家庭ID精确查询
        if (familyId != null) {
            wrapper.eq("family_id", familyId);
        }

        // 缴费年度精确查询
        if (year != null) {
            wrapper.eq("year", year);
        }

        // 缴费人姓名模糊查询
        if (StringUtils.isNotBlank(payerName)) {
            wrapper.like("payer_name", payerName.trim());
        }

        // 排序
        wrapper.orderByDesc("payment_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }

    @Override
    public Page<FamilyPayment> queryPayments(FamilyPayment queryParam, int current, int size) {
        QueryWrapper<FamilyPayment> wrapper = new QueryWrapper<>();

        // 精确查询条件
        if (queryParam.getPaymentId() != null) {
            wrapper.eq("payment_id", queryParam.getPaymentId());
        }

        if (queryParam.getFamilyId() != null) {
            wrapper.eq("family_id", queryParam.getFamilyId());
        }

        if (queryParam.getYear() != null) {
            wrapper.eq("year", queryParam.getYear());
        }

        if (StringUtils.isNotBlank(queryParam.getPayerIdCard())) {
            wrapper.eq("payer_id_card", queryParam.getPayerIdCard());
        }

        if (StringUtils.isNotBlank(queryParam.getPaymentMethod())) {
            wrapper.eq("payment_method", queryParam.getPaymentMethod());
        }

        // 模糊查询条件
        if (StringUtils.isNotBlank(queryParam.getPayerName())) {
            wrapper.like("payer_name", queryParam.getPayerName().trim());
        }

        if (StringUtils.isNotBlank(queryParam.getInvoiceNumber())) {
            wrapper.like("invoice_number", queryParam.getInvoiceNumber().trim());
        }

        // 金额范围查询
        if (queryParam.getTotalAmount() != null) {
            // 这里可以扩展为范围查询，示例中为精确查询
            wrapper.eq("total_amount", queryParam.getTotalAmount());
        }

        // 缴费时间范围查询
        if (queryParam.getPaymentTime() != null) {
            // 示例中只查询大于等于指定时间的记录
            wrapper.ge("payment_time", queryParam.getPaymentTime());
        }

        // 排序
        wrapper.orderByDesc("payment_time");

        // 执行分页查询
        return this.page(new Page<>(current, size), wrapper);
    }

    @Override
    public Map<Integer, BigDecimal> getYearlyPaymentStatistics() {
        QueryWrapper<FamilyPayment> wrapper = new QueryWrapper<>();
        wrapper.select("year", "SUM(total_amount) as total_amount")
                .groupBy("year");

        List<Map<String, Object>> records = this.listMaps(wrapper);
        Map<Integer, BigDecimal> result = new HashMap<>();

        for (Map<String, Object> record : records) {
            Integer year = (Integer) record.get("year");
            BigDecimal amount = new BigDecimal(record.get("total_amount").toString());
            result.put(year, amount);
        }

        return result;
    }
}
